package de.acando.gui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

/**
   The TextEditor class is a simple text editor.
*/

public class TextEditor
{

   
   private String filename;     // To hold the file name
   private JTextArea editorText;// To display the text
   private final int NUM_LINES = 20;  // Lines to display
   private final int NUM_CHARS = 40;  // Chars per line

   /**
      Constructor
   */

   public TextEditor()
   {
      

      // Create the text area.
      editorText = new JTextArea(NUM_LINES, NUM_CHARS);

      // Turn line wrapping on.
      editorText.setLineWrap(true);
      editorText.setWrapStyleWord(true);

      // Create a scroll pane and add the text area to it.
      JScrollPane scrollPane = new JScrollPane(editorText);

   }


   private class NewListener implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
         editorText.setText("");
         filename = null;
      }
   }

   /**
      Private inner class that handles the event that
      is generated when the user selects Open from
      the file menu.
   */

   private class OpenListener implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
         int chooserStatus;

         JFileChooser chooser = new JFileChooser();
         chooserStatus = chooser.showOpenDialog(null);
         if (chooserStatus == JFileChooser.APPROVE_OPTION)
         {
            // Get a reference to the selected file.
            File selectedFile = chooser.getSelectedFile();

            // Get the path of the selected file.
            filename = selectedFile.getPath();

            // Open the file.
            if (!openFile(filename))
            {
               JOptionPane.showMessageDialog(null, 
                                "Error reading " +
                                filename, "Error",
                                JOptionPane.ERROR_MESSAGE);
            }
         }
      }

      /**
         The openFile method opens the file specified by
         filename and reads its contents into the text
         area. The method returns true if the file was
         opened and read successfully, or false if an
         error occurred.
         @param filename The name of the file to open.
      */

      private boolean openFile(String filename)
      {
         boolean success;
         String inputLine, editorString = "";
         FileReader freader;
         BufferedReader inputFile;

         try
         {
            // Open the file.
            freader = new FileReader(filename);
            inputFile = new BufferedReader(freader);

            // Read the file contents into the editor.
            inputLine = inputFile.readLine();
            while (inputLine != null)
            {
               editorString = editorString +
                              inputLine + "\n";
               inputLine = inputFile.readLine();
            }
            editorText.setText(editorString);

            // Close the file.
            inputFile.close();  

            // Indicate that everything went OK.
            success = true;
         }
         catch (IOException e)
         {
            // Something went wrong.
            success = false;
         }

         // Return our status.
         return success;
      }
   }
}